/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.splitor;

import cn.ac.iie.di.global.GlobalParas;
import cn.ac.iie.di.tools.FileTools;
import cn.ac.iie.di.ungzip.UngzipHandler;
import cn.ac.iie.di.ungzip.UngzipTask;
import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class UngzipSplitor implements Runnable {

    String dir = null;
    //String desdir = null;
    String FileString = null;

    static Logger logger = null;

    static {
        logger = org.apache.log4j.Logger.getLogger(UngzipSplitor.class.getName());
    }

    public UngzipSplitor(String dir, String filetype) {
        this.dir = dir;
        //this.desdir = desdir;
        this.FileString = filetype;
    }

    @Override
    public void run() {
        FileTools ft = new FileTools();
        while (true) {
            try {
                logger.info("the original dir is " + dir);
                List<File> gzFiles = ft.getFiles1(new File(dir), "sig");
                HashSet<File> gzSet = new HashSet<>(gzFiles);
                logger.info("================================the Filelist size " + gzSet.size());
                gzFiles.clear();
                gzFiles.addAll(gzSet);
                logger.info("current sig files " + gzFiles);
                //for (File gzFile : gzFiles) {
                for (int i = 0; i < gzFiles.size(); i++) {
                    File gzFile = gzFiles.get(i);
//                    String gzFileName = gzFile.getName();
                    String gzFileName = gzFile.getName().split(".sig")[0];
                    //文件不存在.ok放入队列
//                    File fileok = new File(dir + File.separator + gzFileName.split(".zip")[0] + ".ok");
                    File filelock = new File(dir + File.separator + gzFileName + ".lock");
                    if (!filelock.exists()) {
                        GlobalParas.ungzipQueue.put(dir + File.separator + gzFileName);
                        FileTools.creatTxtFile(filelock.getPath());
                        gzFile.delete();
                    }
                    logger.info("zip file:" + gzFileName);
                }
                logger.debug("GlobalParas.ungzipQueue.size(): " + GlobalParas.ungzipQueue.size());
                Iterator it = GlobalParas.ungzipQueue.iterator();
                while (it.hasNext()) {
                    logger.debug("==========" + it.next().toString());
                }
                Thread.sleep(3000);
            } catch (Exception ex) {
                logger.error("===========================The uncompressed error:" + ex, ex);
            }

        }
    }

    public static void main(String[] args) {
        GlobalParas.ungzipQueue = new ArrayBlockingQueue<String>(500);
        GlobalParas.ORI_NEWS = "D:\\123\\111";
        GlobalParas.TMP_NEWS = "D:\\123\\222";

        UngzipSplitor us = new UngzipSplitor("D:\\123\\111", "");
        Thread t = new Thread(us);
        t.start();
        UngzipHandler h = new UngzipHandler();
        Thread t1 = new Thread(h);
        t1.start();

    }

}
